$(document).ready(function (){
	sessionUrl();
	showsession();
	getSessionUserid();
	
	//读取本地内容，期间显示等待提示
	var thisId = window.location.hash;
	console.log(thisId);
	var postid = $('.postid').attr('postid');
	$.ajax({	//先根据postid读取Postdetail信息
		type:'post',
		url:'../actions/c2cAction.php',
		data:	{
			act		: 	'getpostdetail',
			postid  :	postid
		}, 
		async: true,
		beforeSend:function(){
			showMistAndWait();
		},
		success:function(data){
			var postdetail = eval("("+data+")");;
			postuserid  = postdetail.postuserid;
			$.ajax({	//根据userid读取userdetai信息
				type:'post',
				url:'../actions/userAction.php',
				data:	{
					act		: 	'getuserdetail',
					userid  :	postuserid
				}, 
				async: true,
				beforeSend:function(){
					showMistAndWait();
				},
				success:function(userdet){
					var userdetail = eval("("+userdet+")");
					title 		= postdetail.title;
					content 	= postdetail.content;
					update_time = postdetail.update_time;
					headsrc		= userdetail.headsrc;
					nickname	= userdetail.nickname;
					$('.userhead').attr('src',headsrc);
					$('.postusernick').text(nickname);
					$('.posttime').text(update_time);
					$('.postTitle').append(title);
					$('.postContentShow').append(content);
					//此时post的信息已经展示，再来展示reply们的信息
					
					$.ajax({	//根据userid读取userdetai信息
						type:'post',
						url:'../actions/c2cAction.php',
						data:	{
							act		: 	'getpostallreplies',
							postid  :	postid
						}, 
						async: true,
						beforeSend:function(){
							showMistAndWait();
						},
						success:function(data){
							//console.log('replies的结果是：' + data);
							$('.postreplies').append(data);
							hideMistAndWait();
							if(thisId){
								scrollToId(thisId)
							}
						}
					});
				}
			}); 
		}
	}); 
	
	//点击post中的评论时
	$(document).on("click",".PostReply",function(){
		replyuserid = $(this).attr('user_id');
		var tobereplyid = $(this).attr('replyid');
		currnetuserid = $('.userid').attr('userid');
		replynickname = $(this).find('.r-nickname').text();
		console.log(replynickname);
		if(!currnetuserid){
			var r = confirm("您尚未登录，无法回复评论\n请进入登录页面");
			if(r == true ){
				window.location.href = 'login.php';
			}
		}else{
			if(replyuserid != currnetuserid){
				$('.sbnick').text(replynickname);
				$('.2breplyid').text(tobereplyid);
				$('#reply2sbModal').modal('show');
			}
		}
	});
	
	//modal消失的时候，清空输入框
	$('#reply2sbModal').on('hidden.bs.modal', function (e) {
		$('#reply2sbContent').val('');
	})
	//modal展示后，焦点移动到input内
	$('#reply2sbModal').on('shown.bs.modal', function (e) {
		$('#reply2sbContent').focus();
	})

	//点击回复评论的模态框的回复时
	$('.reply2sbBtn').click(function(){
		var content = $('#reply2sbContent').val();			//校验是否有内容
		tobereplyid = $('.2breplyid').text();
		if(content ==''){
			alert('请填写回复内容');
			$('#reply2sbContent').focus();
		}else{
			console.warn(postid);
			console.warn(tobereplyid);
			console.warn(content);
			  $.ajax({
				type:'post',
				url:'../actions/c2cAction.php',
				data:{
					act			:'reply2sb',
					content		:content,
					replyid		:tobereplyid,
					postsid		:postid
				}, 
				async: true,	
				beforeSend: function(){
					showMistAndWait();
				},
				success:function(data){
						console.log('返回的id'+data);
						alert('回复成功');
						showrelydetail(data);
						//$('.replyinputcontent').val('');
						$('#reply2sbModal').modal('hide');
						hideMistAndWait();
				}
			});      
		}
	});

	//点击回复
	$('.replyBtnDiv').click(function(){
		$.ajax({
		type:'post',
		url:'../actions/userAction.php',
		data:	{
					act			:'checkloged',
				}, 
		async: true,	
		beforeSend:function(){
			$('.waiting').removeClass('hide');
			$('.mist').removeClass('hide');		
		},
		success:function(data){
			if(!data){
				var r = confirm("您尚未登录，点\"确定\"进入登录页面");
				if(r == true ){
					window.location.href = 'login.php';
				}
				$('.waiting').addClass('hide');
				$('.mist').addClass('hide');
			}else{
				replypost();
			}

		}
	});
	
		function replypost(){
			var replyinput = $('.replyinputcontent').val();
			console.log(replyinput);
			if(replyinput == false){
				alert("请输入回复内容");
				$('.waiting').addClass('hide');
				$('.mist').addClass('hide');
			}else{
				$.ajax({	//根据userid读取userdetai信息
					type:'post',
					url:'../actions/c2cAction.php',
					data:	{
						act		:'postreply',
						content :replyinput,
						postid	:postid
					}, 
					async: true,
					beforeSend:function(){
						showMistAndWait();
					},
					success:function(replyid){
						console.log('返回的id'+replyid);
						alert('回复成功');
						showrelydetail(replyid);
						$('.replyinputcontent').val('');
						hideMistAndWait();
					}
				}); 
				
			}
		}
	});
	
	$(document).on('click','.dialoglist',function(){
		console.log(2);
		var postid  = GetQueryString('postid');
		var userid1 = $(this).attr('userid1');
		var userid2 = $(this).attr('userid2');
		$.ajax({	//先根据postid读取Postdetail信息
			type:'post',
			url:'../actions/c2cAction.php',
			data:	{
				act		: 	'getdialoglist',
				postid  :	postid,
				userid1 :	userid1,
				userid2 :	userid2
			}, 
			async: true,
			beforeSend:function(){
				showMistAndWait();
			},
			success:function(data){
				//console.log(data);
				if(data){
					$('.dialoglistcontent').html(data);
					$('#dialoglistmodal').modal('show');
					$('#reply2sbModal').modal('hide');
				}
				hideMistAndWait();
			}
		}); 
	});
	
	
});

//添加回复，并且转动到对应位置
function showrelydetail(replyid){
	var rreplyid = '#reply'+replyid;
	$.ajax({	//根据userid读取userdetai信息
		type:'post',
		url:'../actions/c2cAction.php',
		data:	{
			act		:'showrelydetail',
			replyid	:replyid
		}, 
		async: true,
		beforeSend:function(){
			showMistAndWait();
		},
		success:function(data){
			console.log(data);
			$('.postreplies').prepend(data);
			scrollToId(rreplyid);
		}
	}); 
}


